package com.note.simpleshop.module.goods.service;
import com.note.simpleshop.entity.TbSpec;
import com.note.simpleshop.module.goods.dao.SpecRepository;
import com.note.simpleshop.module.goods.vo.SpecOptionVo;
import com.note.simpleshop.module.goods.vo.SpecVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SpecService {
    private static final String CACHE_NAME="spec_cache";
    @Autowired
    private SpecRepository specRepository;
    @Autowired
    private SpecOptionService specOptionService;
    @Cacheable(CACHE_NAME)
    public List<SpecVo> findByGoodsId(String goodsId)
    {
        List<SpecVo> listVo=new ArrayList<SpecVo>();
        List<TbSpec> list= specRepository.findByGoodsId(goodsId);
        for(TbSpec spec:list)
        {
            SpecVo vo=SpecVo.copy(spec);
            List<SpecOptionVo> specOptionList=specOptionService.findByGoodsIdAndSpecId(goodsId,vo.getId());
            vo.copy(specOptionList);
            listVo.add(vo);
        }
        return listVo;
    }
}
